package repository

import (
	"dataaysn/internal/model"

	"gorm.io/gorm"
)

type MappingRuleRepository interface {
	CreateRule(rule *model.MappingRule) error
	GetRule(id string) (*model.MappingRule, error)
	UpdateRule(rule *model.MappingRule) error
	DeleteRule(id string) error
	ListRules() ([]model.MappingRule, error)
}
type gormMappingRuleRepository struct {
	db *gorm.DB
}

func NewGormMappingRuleRepository(db *gorm.DB) MappingRuleRepository {
	return &gormMappingRuleRepository{db: db}
}

func (r *gormMappingRuleRepository) CreateRule(rule *model.MappingRule) error {
	return r.db.Create(rule).Error
}

func (r *gormMappingRuleRepository) GetRule(id string) (*model.MappingRule, error) {
	var rule model.MappingRule
	err := r.db.First(&rule, id).Error
	return &rule, err
}

func (r *gormMappingRuleRepository) UpdateRule(rule *model.MappingRule) error {
	return r.db.Save(rule).Error
}

func (r *gormMappingRuleRepository) DeleteRule(id string) error {
	return r.db.Delete(&model.MappingRule{}, id).Error
}

func (r *gormMappingRuleRepository) ListRules() ([]model.MappingRule, error) {
	var rules []model.MappingRule
	err := r.db.Find(&rules).Error
	return rules, err
}
